#! /usr/bin/env python
#coding=utf-8
import numpy as np
from PIL import Image
import os
import cv2
# FaceDataPath
Y = []   # ultiY
X = []  # ultiX
img_np=[]
def myget(pa):
    impa=[os.path.join(pa,f) for f in os.listdir(pa)]
    for imp in impa:
        PIL_img=Image.open(imp).convert('L') 
        img_np=np.array(PIL_img,'uint8') 
        X0=det.detectMultiScale(img_np) 
        Y0=int(os.path.split(imp)[1].split('.')[0])  #id
        for (x,y,w,h) in X0: #deletenoface
                X.append(img_np[y:y+h,x:x+w])
                Y.append(Y0)
pas='face'#图片路径
rec=cv2.face.LBPHFaceRecognizer_create()  # model
det=cv2.CascadeClassifier('include/haarcascade_frontalface_alt.xml'
) #facedetector
myget(pas)
rec.train(X,np.array(Y))
rec.write('include/trainer.yml')
print('Done!')
